package com.andaily.web.controller.mobile.log;

import com.andaily.domain.dto.log.LogDto;
import com.andaily.domain.dto.log.LogTypeDto;
import com.andaily.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * @author Shengzhao Li
 */
@Controller
@RequestMapping("/log/form")
public class LogFormController {

    @Autowired
    private LogService logService;

    @RequestMapping(method = RequestMethod.GET)
    public String showForm(String guid, Model model) throws Exception {
        LogDto logDto = logService.loadLogDto(guid);
        model.addAttribute("logDto", logDto);

        List<LogTypeDto> logTypeDtos = logService.loadAvailableLogTypes();
        model.addAttribute("logTypeDtos", logTypeDtos);
        return "log/log_form";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String submit(@ModelAttribute LogDto logDto) throws Exception {
        logService.persistLogDto(logDto);
        return "redirect:" + (logDto.isAddNext() ? "form" : "overview");
    }
}
